package services.snipeit

import javax.inject.Inject
import play.api.libs.json.JsValue
import play.api.{Configuration, Logging}
import play.api.libs.ws.{WSAuthScheme, WSClient}

import scala.concurrent.ExecutionContext

class SnipeITPost @Inject()(ws: WSClient, config: Configuration)(implicit exec: ExecutionContext) extends Logging{
  val freeNASConfig = config.get[SnipeITConfig]("")

  private def postAsUser(api:String,parmant:JsValue) = {
    ws.url(freeNASConfig.url+api).withAuth(username = freeNASConfig.user,password = freeNASConfig.password,scheme = WSAuthScheme.BASIC).post(parmant)
  }

  private def getAsUser(api:String,parmant:String) ={
    ws.url(freeNASConfig.url+api).withAuth(username = freeNASConfig.user,password = freeNASConfig.password,scheme = WSAuthScheme.BASIC).get()
  }

  private def putAsUser(api:String,parmant:JsValue) = {
    ws.url(freeNASConfig.url+api).withAuth(username = freeNASConfig.user,password = freeNASConfig.password,scheme = WSAuthScheme.BASIC).put(parmant)
  }

  private def postAsKey(api:String, parmant:JsValue) = {
    ws.url(freeNASConfig.url+api).withHttpHeaders(("Authorization"->s"Bearer ${freeNASConfig.apikey}")).post(parmant)
  }

  private def getAsKey(api:String, parmant:String) ={
    ws.url(freeNASConfig.url+api).withHttpHeaders(("Authorization"->s"Bearer ${freeNASConfig.apikey}")).get()
  }

  private def putAsKey(api:String,parmant:JsValue) = {
    ws.url(freeNASConfig.url+api).withHttpHeaders(("Authorization"->s"Bearer ${freeNASConfig.apikey}")).put(parmant)
  }

  def post(api:String, parmant:JsValue) =    postAsKey(api:String, parmant:JsValue)

  def get(api:String, parmant:String) =    getAsKey(api:String, parmant:String)

  def put(api:String,parmant:JsValue) =    putAsKey(api:String,parmant:JsValue)

}
